package com.tanhua.sso.interceptor;

import com.tanhua.common.enums.LogTypeEnum;
import com.tanhua.common.pojo.Log;
import com.tanhua.common.pojo.User;
import com.tanhua.common.service.LogService;
import com.tanhua.common.utils.UserThreadLocal;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogInterceptor implements HandlerInterceptor {

    private LogService logService;

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        if(((HandlerMethod) handler).hasMethodAnnotation(com.tanhua.common.utils.Log.class)){
            User user = UserThreadLocal.get();
            Log log = new Log();
            log.setId(user.getId());
            log.setLogTime(System.currentTimeMillis());
            log.setType(LogTypeEnum.LOGIN);
            log.setEquipment("iphone");
            log.setPlace("合肥");
            logService.saveLog(log);
        }

        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
